function L = df_formL(N,P,A,V)
%period boundary condtion, form the sparse matrix L
%domain [0,1]^2
%form: -div(A(x) grad u) + V(x) u 
%A is an N by N matrix 
%V is an N by N matrix
  
  h = 1/N;
  
  idx = reshape(1:N*N,N,N);
  MD = 1:N;
  LF = [N 1:N-1];
  RT = [2:N 1];
  
  Il = idx(MD,MD);
  Jl = idx(LF,MD);
  Sl = -1/(h*h)*(A(MD,MD)+A(LF,MD))/2;
  
  Ir = idx(MD,MD);
  Jr = idx(RT,MD);
  Sr = -1/(h*h)*(A(MD,MD)+A(RT,MD))/2;

  Id = idx(MD,MD);
  Jd = idx(MD,LF);
  Sd = -1/(h*h)*(A(MD,MD)+A(MD,LF))/2;
  
  Iu = idx(MD,MD);
  Ju = idx(MD,RT);
  Su = -1/(h*h)*(A(MD,MD)+A(MD,RT))/2;
  
  Im = idx(MD,MD);
  Jm = idx(MD,MD);
  Sm = -(Sl+Sr+Su+Sd) + V(MD,MD);
  
  Is = [Il(:); Ir(:); Id(:); Iu(:); Im(:)];
  Js = [Jl(:); Jr(:); Jd(:); Ju(:); Jm(:)];
  Ss = [Sl(:); Sr(:); Sd(:); Su(:); Sm(:)];
  
  L = sparse(Is,Js,Ss);